package com.sun.androidexerciseproject.function.security;

import android.util.Base64;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by Administrator on 2018/1/26 0026.
 */

public class CipherTest {
    static Cipher sCipher;

    static {
        try {
            sCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        CipherTest cipherTest = new CipherTest();
//        cipherTest.encode("TestMessage");
        try {
            rsaTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void rsaTest() throws Exception {
        Map<String, String> map = RSAUtil.generateKeyPair();
        String pulicKey = map.get("publicKey");
        String privateKey = map.get("privateKey");
        if ((null != pulicKey) && (null != privateKey)) {
            String plainText = "";
            System.out.println("publicKey = " + pulicKey + " size  = " + pulicKey.length());
            System.out.println("privateKey = " + privateKey + " size  = " + privateKey.length());
            for (int i = 0; i < 117; i++) {
                plainText += 1;
            }
            int len = plainText.getBytes().length;
            System.out.println("len = " + len);
            System.out.println("plainText = " + plainText + " len = " + plainText.length());
            String encryptText = RSAUtil.encrypt(plainText, pulicKey);
            String decryptText = RSAUtil.decrypt(encryptText, privateKey);
            String sign = RSAUtil.sign(plainText, privateKey);
            System.out.println("sign = " + sign + " size = " + sign.length());
            System.out.println("plainText = " + plainText);
            System.out.println("encryptText = " + encryptText + " size = " + encryptText.length());
            System.out.println("decryptText = " + decryptText + " size = " + decryptText.length());
        }
    }

    private String getKey() {
        String key;
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            SecretKey secretKey = keyGenerator.generateKey();
            key = Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT);
            return key;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String encode(String message) {
        String key = getKey();
        String result;
        System.out.println("key = " + key);
        try {
            sCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            result = Base64.encodeToString(sCipher.doFinal(message.getBytes()), Base64.DEFAULT);
            return result;
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }
}
